package com.example.multiactivity

import androidx.activity.enableEdgeToEdge
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.widget.TextView
import android.widget.Button
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import org.w3c.dom.Text
import java.util.Locale

// 全局变量
var seconds = 0; // 记录运行了多少秒
var running = false; // running控制是否开始计时
var wasRunning = false; //

private lateinit var displyTimeText: TextView;
class fifthActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_fifth)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        // 获取 button
        val startButton = findViewById<Button>(R.id.startButton);
        val stopButton = findViewById<Button>(R.id.stopButton);
        val restartButton = findViewById<Button>(R.id.restartButton);
        displyTimeText = findViewById<TextView>(R.id.displyTimeText);

        if (savedInstanceState != null) {
            seconds = savedInstanceState.getInt("seconds");
            running = savedInstanceState.getBoolean("running");
            wasRunning = savedInstanceState.getBoolean("wasRunning");
        }

        // 计时开始
        runTimer();

        // 事件监听
        startButton.setOnClickListener {
            running = true;
        }

        stopButton.setOnClickListener {
            running = false;
        }

        restartButton.setOnClickListener {
            running = true;
            seconds = 0; // 重置为 0
        }
    }

    fun runTimer() {
        val handler = Handler(Looper.getMainLooper())

        val runnable = object : Runnable {
            override fun run() {
                val hours = com.example.multiactivity.seconds / 3600
                val minutes = (com.example.multiactivity.seconds % 3600) / 60
                val secs = com.example.multiactivity.seconds % 60
                // 格式化为 HH:MM:SS
                com.example.multiactivity.displyTimeText.text = String.format("%02d:%02d:%02d", hours, minutes, secs)
                if (com.example.multiactivity.running) {
                    com.example.multiactivity.seconds++
                }
                handler.postDelayed(this, 1000)// 每隔 1000ms (1秒) 再次调用自己
            }
        }
        handler.post(runnable)// 启动定时器：第一次调用 runnable
    }

    override fun onSaveInstanceState(outState: Bundle){
        super.onSaveInstanceState(outState)
        Log.d( "Life cycle","onSaveInstanceState")
        outState.putInt("seconds", com.example.multiactivity.seconds)
        outState.putBoolean("running", com.example.multiactivity.running)
        outState.putBoolean("wasRunning", com.example.multiactivity.wasRunning)
    }
}